class_name Ark_Player

extends CharacterBody2D


const SPEED = 150.0
const JUMP_VELOCITY = -0.0

var companion: Array[Npc] = []

var _controllable: bool = true
var controllable: 
	set(value):
		if not value:
			velocity.x = 0
		_controllable = value
	get():
		return _controllable
		
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var animation: AnimationPlayer = $AnimationPlayer

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
	
	if controllable:
		# Handle jump.
		#if Input.is_action_just_pressed("ui_accept") and is_on_floor():
			#velocity.y = JUMP_VELOCITY
		var direction := Input.get_axis("ui_left", "ui_right")
		if direction:
			velocity.x = direction * SPEED
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED)

		if direction > 0:
			sprite_2d.flip_h = true
		elif direction < 0:
			sprite_2d.flip_h = false
			
	if not is_zero_approx(velocity.x):
		animation.play("running")
		for cp in companion:
			cp.position.x = position.x + cp.rel_dist
			cp.animation.play("running")
	elif animation.current_animation == "running":
		animation.play("RESET")
		for cp in companion:
			cp.position.x = position.x + cp.rel_dist
			cp.animation.play("idle")
	
	
		
	move_and_slide()
